package com.unchartedskies.model.service
{
	import com.unchartedskies.controller.event.PlayerIOEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	
	import org.robotlegs.mvcs.Actor;
	
	import playerio.Client;
	import playerio.Connection;
	import playerio.PlayerIO;
	import playerio.PlayerIOError;
	
	public class PlayerIOService extends Actor
	{
		private var client:Client;
		private var connection:Connection;
		
		public function connect(stage:Stage):void
		{
			dispatch(new PlayerIOEvent(PlayerIOEvent.CONNECTING,""));
			PlayerIO.connect(
				stage,								//Referance to stage
				"unchartedskies-realtime-y97gfjgb90wtuxjbmqe6xa",			//Game id (Get your own at playerio.com)
				"public",							//Connection id, default is public
				"GuestUser",						//Username
				"",									//User auth. Can be left blank if authentication is disabled on connection
				null,								//Current PartnerPay partner.
				handleConnect,						//Function executed on successful connect
				handleError							//Function executed if we recive an error
			);  
		}
		private function handleConnect(client:Client):void
		{
			this.client = client;
			dispatch(new PlayerIOEvent(PlayerIOEvent.CONNECTED,""));
		}
		private function handleError(e:PlayerIOError):void
		{
			dispatch(new PlayerIOEvent(PlayerIOEvent.ERROR,e.message));
		}
	}
}